//
//  ArtOperationView.m
//  jingdongfang
//
//  Created by MTiMac02 on 2018/5/16.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "ArtOperationView.h"
#import "KLCPopup.h"
#import "IGAddToDrawVC.h"
#import "YXScrollowActionSheet.h"
#import "IGCommentVC.h"
#import "IGSimilarArtVC.h"
#import "IntroViewController.h"
#import "ArtGalleryVC.h"
#import "IGiGalleryListApi.h"
#import "NotFindDeviceView.h"
#import "IGMyiGalleryVC.h"
#import "IGPushiGalleryListVC.h"
#import "IGiGalleryPushApi.h"
#import "IGArtistVC.h"
#import "IGSearchVC.h"

#define Start_X          10.0f      // 第一个按钮的X坐标
#define Start_Y          50.0f     // 第一个按钮的Y坐标
#define Width_Space      5.0f      // 2个按钮之间的横间距
#define Height_Space     20.0f     // 竖间距
#define Button_Height   122.0f    // 高
#define Button_Width    75.0f    // 宽
#define ColumnNum 4

@interface ArtOperationView()
{
    NSArray *nameArray;
    NSArray *iconArray;
    UIView *bottomView;
    UIView *labelView;
    NSString *_name;
    NotFindDeviceView *findView;
}
@property (nonatomic, strong) IGArtWorkModel *artWork;
@property (nonatomic, strong) IGiGalleryListApi *igalleryListApi;
@property (nonatomic, strong) NSArray *iGalleryListArr;

@end

@implementation ArtOperationView
+ (ArtOperationView *)showArtOperationViewArtWork:(IGArtWorkModel *)artWork {
    ArtOperationView *operationView = [[ArtOperationView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) name:artWork.title];
    operationView.backgroundColor = [UIColor clearColor];
    operationView.artWork = artWork;
    
    [operationView setTapActionWithBlock:^{
        [KLCPopup dismissAllPopups];
    }];
    
    [operationView show];
    return operationView;
}

-(instancetype)initWithFrame:(CGRect)frame name:(NSString *)name{
    self = [super initWithFrame:frame];
    if (self) {
        [self initViewName:name];
    }
    return  self;
}

-(void)initViewName:(NSString *)name{
    bottomView=[[UIView alloc]initWithFrame:CGRectMake(0, KSCREENHEIGHT - 250, KSCREENWIDTH, 250)];
    bottomView.backgroundColor=[UIColor whiteColor];
    bottomView.bottom=self.bottom;
    _name=name;
    [self addSubview:bottomView];
    [self setTitleView];
    [self setButtonView];
}

-(void)setTitleView{
    labelView=[[UIView alloc]initWithFrame:CGRectZero];
    labelView.backgroundColor=[UIColor whiteColor];
    labelView.frame=CGRectMake(0, 0, KSCREENWIDTH, 50);
    [bottomView addSubview:labelView];
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectZero];
    name.text=[NSString stringWithFormat:@"作品名: %@",_name];
    name.textColor=[UIColor c4Color];
    name.textAlignment=NSTextAlignmentLeft;
    name.font=[UIFont boldSystemFontOfSize:19];
    name.frame=CGRectMake(15, 10, KSCREENWIDTH-15, 30);
    [labelView addSubview:name];
    UILabel *line=[[UILabel alloc]init];
    line.backgroundColor=[UIColor c5Color];
    line.frame=CGRectMake(15, labelView.bottom-0.5, KSCREENWIDTH-15, 0.5);
    [labelView addSubview:line];
}
-(void)setButtonView{
    UIView *buttonView=[[UIView alloc]init];
    buttonView.backgroundColor=[UIColor whiteColor];
    buttonView.frame=CGRectMake(0, labelView.bottom, KSCREENWIDTH, 200);
    [bottomView addSubview:buttonView];
    nameArray=[NSArray arrayWithObjects:@"推送到画屏",@"添加到画单",@"分享",@"评论",@"艺术家",@"艺术馆",@"相似画作",nil];
    iconArray=[NSArray arrayWithObjects:@"more-push",@"more-add",@"more-share",@"more-talk",@"more-artist",@"more-musuem",@"more-similar",nil];
    CGFloat btnW = 75;
    CGFloat btnH = 75;
    int marginY = 22;
    CGFloat firstY = 10;
    for (int i=0; i<nameArray.count; i++) {
        ArtOperationButton *btn = [ArtOperationButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        [btn setTitle:nameArray[i] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:iconArray[i]] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        CGFloat marginX = (buttonView.frame.size.width - ColumnNum * btnW) / (ColumnNum + 1);
        int col = i % ColumnNum;
        int row = i / ColumnNum;
        CGFloat btnX = marginX + (marginX + btnW) * col;
        CGFloat btnY = firstY + (marginY + btnH) * row;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [buttonView addSubview:btn];
    }
}

- (void)show {
    KLCPopup *pop = [KLCPopup popupWithContentView:self showType:KLCPopupShowTypeSlideInFromBottom dismissType:KLCPopupDismissTypeFadeOut maskType:KLCPopupMaskTypeDimmed dismissOnBackgroundTouch:YES dismissOnContentTouch:NO];
    [pop show];
}

- (void)btnClick:(UIButton *)btn {
    [KLCPopup dismissAllPopups];
    if (btn.tag==0) {
        [self pushPhoto:btn];
    } else if (btn.tag==1) {
        IGAddToDrawVC *addToDrawVC = [[IGAddToDrawVC alloc] init];
        addToDrawVC.workIds = @[self.artWork.aId];
        [UIViewController ig_pushViewController:addToDrawVC animated:YES];        
    } else if (btn.tag==2) {
        YXScrollowActionSheet *cusSheet = [[YXScrollowActionSheet alloc] init];
        IGShareModel *shareModel = [self.artWork getShareModel];
        cusSheet.shareModel = shareModel;
        [cusSheet show];
    } else if (btn.tag==3) {
        IGCommentVC *commentVC = [[IGCommentVC alloc] initWithType:IGCommentPageTypeZuopin baseModel:self.artWork];
        [UIViewController ig_pushViewController:commentVC animated:YES];
    } else if (btn.tag==4) {
        if ([self.artWork.userType isEqualToString:@"艺术家"]||[self.artWork.userType isEqualToString:@"个人"]){
        IntroViewController *artistVC=[[IntroViewController alloc]init];
        artistVC.u_id=self.artWork.uploaderId;
        [UIViewController ig_pushViewController:artistVC animated:YES];
        }else{
            IGSearchVC *searchVc=[[IGSearchVC alloc]initWithSearchKey:self.artWork.title];
            searchVc.defaulSel=@"1";
            [UIViewController ig_pushViewController:searchVc animated:YES];
        }
    } else if (btn.tag==5) {
        if ([self.artWork.userType isEqualToString:@"艺术馆"]&&self.artWork.uploaderId!=nil){
        ArtGalleryVC *museumVC=[[ArtGalleryVC alloc]init];
            museumVC.aId=self.artWork.uploaderId?self.artWork.uploaderId:@"0";
        [UIViewController ig_pushViewController:museumVC animated:YES];
        }else{
            IGSearchVC *searchVc=[[IGSearchVC alloc]initWithSearchKey:self.artWork.title];
            searchVc.defaulSel=@"1";
            [UIViewController ig_pushViewController:searchVc animated:YES];
        }
    } else if (btn.tag==6) {
        IGSimilarArtVC *similarVc=[[IGSimilarArtVC alloc]init];
        similarVc.pictureId=self.artWork.aId;
        [UIViewController ig_pushViewController:similarVc animated:YES];
    }
    
}
#pragma mark ===== 推送至画屏
- (void)pushPhoto:(UIButton *)btn
{
    self.igalleryListApi = [[IGiGalleryListApi alloc] initWithType:IGiGalleryListTypeSuccess];
    __weak typeof(self) weakSelf = self;
    [self.igalleryListApi startWithDecodeClass:[IGiGalleryListResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGiGalleryListResp *resp = responseObject;
            IGiGalleryListModel *model = resp.data;
            weakSelf.iGalleryListArr = [NSArray arrayWithArray:model.equs];
            if (weakSelf.iGalleryListArr.count == 0)
            {
                findView = [NotFindDeviceView showNotFindDeviceView];
                findView.bangdingBlock = ^{
                    [UIViewController ig_pushViewControllerClass:[IGMyiGalleryVC class] animated:YES];
                };
            }
            else if (weakSelf.iGalleryListArr.count == 1)
            {
                [weakSelf pushToDev];
            }
            else
            {
                IGPushiGalleryListVC *listVC = [[IGPushiGalleryListVC alloc] initWithType:IGPushiGalleryType1N];
                listVC.recommend = _artWork;
                [UIViewController ig_pushViewController:listVC animated:YES];
            }
        }
    }];
}


- (void)pushToDev
{
    [self presentMessageTips:NSLocalizedString(@"Pushing...", nil)];
    IGiGalleryEqu *equ = self.iGalleryListArr[0];
    IGiGalleryPushApi *pushApi = [[IGiGalleryPushApi alloc] initWithMacIds:@[equ.macId] pIds:@[_artWork.aId]];
    //    [self presentMessageTips:@"正在推送..."];
    [pushApi startWithDecodeClass:[IGiGalleryPushResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            //            [self presentMessageTips:@"推送成功..."];
            [self presentMessageTips:NSLocalizedString(@"Pushed Successfully...\n Would show up until device online.", nil)];
        }
    }];
}

@end

#define kTitlePrecent 0.4
#define kImageViewWH 50
@implementation ArtOperationButton


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont font14];
        [self setTitleColor:[UIColor c4Color] forState:UIControlStateNormal];
    }
    return self;
}

#pragma mark 调整文字的位置和尺寸
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGFloat titleW = self.frame.size.width;
    CGFloat titleH = self.frame.size.height * kTitlePrecent;
    CGFloat titleX = 0;
    CGFloat titleY = self.frame.size.height * (1 - kTitlePrecent);
    return CGRectMake(titleX, titleY, titleW, titleH);
}

#pragma mark 调整图片的位置和尺寸
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
    CGFloat imageW = kImageViewWH;
    CGFloat imageH = kImageViewWH;
    CGFloat imageX = (self.frame.size.width - kImageViewWH) * 0.5;
    CGFloat imageY = 0;
    return CGRectMake(imageX, imageY, imageW, imageH);
}

@end
